package com.breaking.second.activity.pager.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.breaking.second.activity.SearchActivity;
import com.breaking.second.activity.pager.BaseFragment;
import com.breaking.second.activity.pager.adapter.FragmentStateAdapter;
import com.breaking.second.databinding.PagerHomeBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.GoodsTypeEntity;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

//首页全部商品列表，分类
public class HomePager extends BaseFragment<PagerHomeBinding> implements TabLayout.OnTabSelectedListener {
    private final List<Fragment> fragments = new ArrayList<>();
    private FragmentStateAdapter fragmentStateAdapter;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initPager();
        initTab();
        initData();
    }

    private void initView() {
        binding.search.setOnClickListener(this::search);
    }

    private void search(View view) {
        startActivity(new Intent(requireContext(), SearchActivity.class));
    }

    private void initTab() {
        binding.tab.addOnTabSelectedListener(this);
    }

    private void initPager() {
        fragmentStateAdapter = new FragmentStateAdapter(getChildFragmentManager(), getLifecycle(), fragments);
        binding.pager.setAdapter(fragmentStateAdapter);
        binding.pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                TabLayout.Tab tab = binding.tab.getTabAt(position);
                if (tab == null) return;
                tab.select();
            }
        });
    }

    private void initData() {
        getTypeData();//初始化分类数据
    }

    private void getTypeData() {
        OkhttpUtil.Get("goods/type", new OkhttpUtil.CallbackListener(this::getTypeData));
    }

    private void getTypeData(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            List<GoodsTypeEntity> goodsTypeEntities = bookEntity.toList(GoodsTypeEntity.class);
            if (goodsTypeEntities == null) return;
            if (goodsTypeEntities.isEmpty()) return;
            setTabPagerData(goodsTypeEntities);
        }
    }

    private void setTabPagerData(List<GoodsTypeEntity> goodsTypeEntities) {
        binding.tab.removeAllTabs();
        fragmentStateAdapter.notifyItemRangeRemoved(0, fragments.size() + 1);
        fragments.clear();
        for (GoodsTypeEntity entity : goodsTypeEntities) {
            binding.tab.addTab(binding.tab.newTab().setText(entity.getType()));
            fragments.add(new GoodsPager(entity));
        }
        fragmentStateAdapter.notifyItemRangeChanged(0, fragments.size());
        binding.pager.setOffscreenPageLimit(fragments.size());
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        binding.pager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
